package serializare;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import bean.Person;

import core.controller.ServletParameters;

public final class SerializationToXml {
    private static final String PATH = ServletParameters.getProjectPath() + "serialize.xml";

    private SerializationToXml() {
    }

    public static void serializeObjectToXml(List<Person> persoane) {
        XMLEncoder encoder = null;
        try {
            encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(PATH)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        encoder.writeObject(persoane);
        encoder.close();
    }

    public static List<Person> deserializeXMLToObject() {
        XMLDecoder decoder = null;
        try {
            decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(PATH)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<Person> persoane = (List<Person>) decoder.readObject();
        decoder.close();
        return persoane;
    }
}